package OBS;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.GregorianCalendar;

/**
 *  En klasse der bruges til, at sammenligne tidspunkter på batchrapporter, når de bliver vist i en liste.
 *
 * @author Daniel Koch
 */
public class CompareBatchReportListDate implements Comparator<String[]> {
    /**
     *
     * @param item1
     * @param item2
     * @return
     */
    public int compare(String[] item1, String[] item2) {
        GregorianCalendar gc1 = stringToDate(item1[2]);
        GregorianCalendar gc2 = stringToDate(item2[2]);
        if(gc1 == null) {
            return 0;
        }
        if(gc2 == null) {
            return 1;
        }
        return gc2.compareTo(gc1);
    }
    /**
     * 
     * @param date
     * @return
     */
    private GregorianCalendar stringToDate(String date) {
        GregorianCalendar gc = new GregorianCalendar();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            gc.setTime(format.parse(date));
            return gc;
        }
        catch(Exception e) {
            e.printStackTrace();
            return new GregorianCalendar();
        }
    }
}
